package com.lions.client.view.buttonpanel;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.lions.client.controller.Speech;
import com.lions.client.model.AudioOutput;
import com.lions.client.model.Model;
import com.lions.client.model.Module;
import com.lions.client.view.mappanel.MapPanel;
import com.lions.client.view.modulepanel.EditModulePopup;
import com.lions.client.view.modulepanel.ModulePanel;
import com.lions.client.view.modulepanel.ModulePopup;
import com.lions.client.view.popup.MyPopupPanel;

public class ButtonPanel extends HorizontalPanel{

	public ButtonPanel(Model m, MapPanel mapP, ModulePanel moduleP) {
		super();
		model = m;
		mapPanel = mapP;
		modulePanel = moduleP;
		speech = new Speech(this);
		
		
		// Initializes all of the components for this panel
		setHorizontalAlignment(HasAlignment.ALIGN_CENTER);
		addStyleName("controlpanel");
		// Add items to the panel
		add(speech);
		addModuleButton = new Button("Add Module");
		addModuleButton.setStyleName(buttonStyle);
		editModuleButton = new Button("Edit Module");
		editModuleButton.setStyleName(buttonStyle);
		removeModuleButton = new Button("Remove Module");
		removeModuleButton.setStyleName(buttonStyle);
		add(addModuleButton);
		add(editModuleButton);
		add(removeModuleButton);
		buildBaseButton = new Button("Build Selected Base");
		buildBaseButton.setStyleName(buttonStyle);
		editBaseButton = new Button("Edit Current Base");
		editBaseButton.setStyleName(buttonStyle);
		add(buildBaseButton);
		add(editBaseButton);
//		buildBaseButton.addClickHandler(new ClickHandler() {
//			@Override
//			public void onClick(ClickEvent event) {
//				if (model.checkMin() == true) {
//
//				} else {
//					new MyPopupPanel("Base Configuration",
//							"Minimum Base Configuration is Not Available\n");
//					new AudioOutput().playBaseError();
//				}
//			}
//		});
		
		final int ZERO = 0;
		// Add module add button click handler
		addModuleButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				ModulePopup popup = new ModulePopup(modulePanel, mapPanel);
				popup.center();
				popup = null;
			}
		});

		// Add delete module button click handler
		removeModuleButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				ListBox visible = modulePanel.getVisibleListBox();
				if (visible != null) {
					int modIndex = visible.getSelectedIndex();
					if (modIndex >= 0) {
						String modString = visible.getItemText(modIndex);
						//## Ycord: ##
						modString = modString.substring(modString.indexOf(':') + 2);
						int xCoord = Integer.parseInt(modString.substring(ZERO, modString.indexOf(" ")));
						//##
						modString = modString.substring(modString.indexOf(':') + 2);
						int yCoord = Integer.parseInt(modString);
						// Delete module
						modulePanel.removeModule(xCoord, yCoord);
					} else {
						new MyPopupPanel("Select Module", "Please Select a Module.");
						new AudioOutput().playInputError();
					}
				} else {
					new MyPopupPanel("Select Module", "Please Select a Module.");
					new AudioOutput().playInputError();
				}
			}
		});
		
		// Add edit module button click handler
		editModuleButton.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				ListBox visible = modulePanel.getVisibleListBox();
				if (visible != null) {
					int modIndex = visible.getSelectedIndex();
					if (modIndex >= 0) {
						String modString = visible.getItemText(modIndex);
						//## Ycord: ##
						modString = modString.substring(modString.indexOf(':') + 2);
						int xCoord = Integer.parseInt(modString.substring(ZERO, modString.indexOf(" ")));
						//##
						modString = modString.substring(modString.indexOf(':') + 2);
						int yCoord = Integer.parseInt(modString);
						// Edit Module
						if (model.getModuleList().getModuleAtCoord(xCoord, yCoord) != null) {
							Module mod = model.getModuleList().getModuleAtCoord(xCoord, yCoord);
							EditModulePopup popup = new EditModulePopup(modulePanel, mapPanel, mod);
							popup.center();
							popup = null;
						} else {
							new MyPopupPanel("Error", "Error: Could Not Find Module");
							new AudioOutput().playBaseError();
						}
						// Redraw modList and mapPanel
					} else {
						new MyPopupPanel("Select Module", "Please Select a Module.");
						new AudioOutput().playInputError();
					}
				} else {
					new MyPopupPanel("Select Module", "Please Select a Module.");
					new AudioOutput().playInputError();
				}			
			}
		});
	}
	
	/**
	 * Returns the actual button to add actions listeners.
	 * 
	 * @return
	 */
	public Button getAddButton() {
		return addModuleButton;
	}

	/**
	 * Returns the actual button to add actions listeners.
	 * 
	 * @return
	 */
	public Button getEditButton() {
		return editModuleButton;
	}

	/**
	 * Returns the actual button to add actions listeners.
	 * 
	 * @return
	 */
	public Button getModuleRemoveButton() {
		return removeModuleButton;
	}

	/**
	 * Returns the actual button to add actions listeners.
	 * 
	 * @return
	 */
	public Button getBuildBaseButton() {
		return buildBaseButton;
	}

	/**
	 * Returns the actual button to add actions listeners.
	 * 
	 * @return
	 */
	public Button getEditBaseButton() {
		return editBaseButton;
	}
	
	public Speech getSpeech() {
		return speech;
	}
	
	private String buttonStyle = "buttonstyle";
	private Button addModuleButton;
	private Button editModuleButton;
	private Button removeModuleButton;
	private Button buildBaseButton;
	private Button editBaseButton;
	private Speech speech;
	private Model model;
	private ModulePanel modulePanel;
	private MapPanel mapPanel;
}
